package com.framework.service.yhbservice.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.YHBDao.currentcountDao.YhbCurrentCountDao;
import com.framework.page.PageUtils;
import com.framework.service.yhbservice.YhbCurrentCountService;
import com.framework.service.yhbservice.YhbIncomeTypeService;
import com.framework.service.yhbservice.YhbUserService;
import com.framework.utils.string.StringUtil;
import com.framework.yhbEntity.currentcount.YhbCurrentCount;
import com.framework.yhbEntity.currentcount.YhbIncomeType;
import com.framework.yhbEntity.user.YhbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账单明细
 */
@Transactional(rollbackFor = Exception.class)
@Service("yhbCurrentCountService")
public class YhbCurrentCountServiceImpl extends ServiceImpl<YhbCurrentCountDao,YhbCurrentCount> implements YhbCurrentCountService {

    @Autowired
    private YhbUserService yhbUserService;

    @Autowired
    private YhbIncomeTypeService yhbIncomeTypeService;

    @Override
    public List<Map<String, Object>> getCurrentCountInfoList(String token, Integer pageNo) {
        YhbUser user = yhbUserService.queryUserByToken(token);
        Integer start=10*(pageNo-1);
        List<Map<String, Object>>  list=baseMapper.getCurrentCountInfoList(user.getUserId(),start);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryFreezenFlow(String token, Integer pageNo) {

        YhbUser user = yhbUserService.queryUserByToken(token);
        Integer start=10*(pageNo-1);
        return baseMapper.queryFreezenFlow(user.getUserId(),start);
    }

    @Override
    public void addRegisterCount(String userId) {
        String accountId= StringUtil.getDateString(6);
        YhbCurrentCount yhbCurrentCount=new YhbCurrentCount();
        yhbCurrentCount.setAccountId(accountId);
        yhbCurrentCount.setUserId(userId);
        yhbCurrentCount.setIsActivity(0);
        yhbCurrentCount.setMoney(new BigDecimal("0.00"));
        yhbCurrentCount.setIncome(1);
        yhbCurrentCount.setIncomeType(4);
        yhbCurrentCount.setAmount(new BigDecimal("10"));
        yhbCurrentCount.setRelationId("N");
        yhbCurrentCount.setCreateDate(new Date());
        baseMapper.insert(yhbCurrentCount);
    }

    @Override
    public PageUtils queryCurentCountPage(Map<String, Object> params) {
        params.put("startTime", params.get("startTime"));
        params.put("endTime", params.get("endTime"));
        Integer currpage = Integer.valueOf(params.get("page").toString());
        Integer pageSize = Integer.valueOf(params.get("limit").toString());
        params.put("start", (currpage - 1) * pageSize);
        params.put("limit", pageSize);
        List<Map<String,Object>> list=baseMapper.queryCurrentCountlist(params);
        Integer count=baseMapper.queryCurrentCountCount(params);
        return new PageUtils(list, count, pageSize, currpage);
    }

    @Override
    public List<YhbIncomeType> getIncomeType() {
        List<YhbIncomeType> list = yhbIncomeTypeService.selectList(new EntityWrapper<>());
        return list;
    }
}
